﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMS.SMSH.MODEL;
using LMS.SMSH.BLL;
using System.Data.Linq;
using System.Data;

namespace LMS.SMSH.WEB
{
    public partial class AdmTestItemManange : System.Web.UI.Page
    {
        int rowNo = 0; //行编号


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 绑定gvProject数据
                binggvProject();
            }
        }

        /// <summary>
        /// 绑定gvProject数据
        /// </summary>
        private void binggvProject()
        {
            gvProject.DataSource = LMSDataContext.GetLMSDB.sp_viewPro();
            gvProject.DataKeyNames = new string[] { "ID" };
            gvProject.DataBind();
        }

        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            string name = txtProName.Text.Trim();
            string id = txtProID.Text.Trim();
            if (!string.IsNullOrEmpty(id) || !string.IsNullOrEmpty(name))
            {
                //只要有一个输入值不为空就行
                var query = LMSDataContext.GetLMSDB.TB_Project.Where(p => p.proId.IndexOf(id) >= 0
                                                                  && p.proName.IndexOf(name) >= 0);
                gvProject.DataSource = query;
                gvProject.DataKeyNames = new string[] { "proId" };
                gvProject.DataBind();
            }
            else
            {
                //两个输入值都为空
                binggvProject();
            }
        }

        /// <summary>
        /// 添加一条实验记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddTestInfo_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdmTestStuManage.aspx");
        }


        /// <summary>
        /// 行数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvProject_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标悬停变色
                e.Row.Attributes.Add("onmouseover", "oldBG=this.style.backgroundColor;this.style.backgroundColor='#FE18e2';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=oldBG;");

                //删除确认
                Button btnmyDelete = (Button)e.Row.FindControl("btnDeleteInfo");
                btnmyDelete.Attributes.Add("onclick", "return confirm('确定要删除当前记录吗?');");

                ///行编号
                rowNo++;
                e.Row.Cells[0].Text = (gvProject.PageIndex * gvProject.PageSize + rowNo).ToString();
            }
        }



        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvProject.PageIndex = e.NewPageIndex;
            binggvProject();
        }

        /// <summary>
        /// 全选、反选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void chkHeader_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow gr in gvProject.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                //相互变幻
                chkItem.Checked = !chkItem.Checked;
            }
        }


        /// <summary>
        /// 行命令，删除选中行留言
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvProject_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            TB_Project pro = new TB_Project();
            foreach (GridViewRow gr in gvProject.Rows)
            {
                CheckBox chkItem = (CheckBox)gr.FindControl("chkItem");
                if (chkItem.Checked)
                {
                    pro.ID = Int32.Parse(e.CommandArgument.ToString());
                    //调用方法删除选择行数据
                    LMSDataContext.GetLMSDB.TB_Project.DeleteOnSubmit(pro);
                    LMSDataContext.GetLMSDB.SubmitChanges();
                }
            }
            binggvProject();//再次绑定数据
        }


    }
}